package com.liufh.controller;

import com.liufh.common.ResultData;
import com.liufh.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author: liufh
 * @create: 2022-01-21 09:54
 **/
@RestController
@RequestMapping("/api/code")
public class CodeController {

    @Autowired
    private CodeService codeService;

    /**
     * ⽣成验证码
     */
    @PostMapping("/create/{email}")
    public ResultData create(@PathVariable("email") String email, HttpServletResponse response) throws Exception {
        System.out.println("====create email:" + email);
        codeService.createCode(email);
        return ResultData.success(response, "ok", "success", "0");
    }

    /**
     * 校验验证码是否正确
     */
    @PostMapping("/validate/{email}/{code}")
    public ResultData validate(@PathVariable("email") String email, @PathVariable("code") String code, HttpServletResponse response) throws Exception {
        System.out.println("====validate email:" + email);
        String resCode = codeService.validateCode(email, code);
        return ResultData.success(response, "ok", "success", resCode);
    }

}
